package com.hrt.datasender.web.monitordata;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.stereotype.Component;

import com.btcode.db.result.Row;
import com.hrt.datasender.datadao.model.BcDeviceMonitorParamModel;
import com.hrt.datasender.datadao.service.BcDeviceMonitorParamService;

@Component
public class MonitorDataFormater {
    private final int defaultPointNum = 3;

    private static MonitorDataFormater instance;

    public static MonitorDataFormater getInstance() {
        if (instance == null) {
            instance = new MonitorDataFormater();
        }
        return instance;
    }

    /**
     * 地表水测量值小数位保留规则
     */
    private HashMap<String, Integer> pollNum;

    private List<String> polls;

    public MonitorDataFormater() {
        resetPolls();
    }

    public void resetPolls() {
        pollNum = new HashMap<>();
        polls = new ArrayList<>();

        List<BcDeviceMonitorParamModel> models = BcDeviceMonitorParamService.getInstance().getParamByStation();

        for (BcDeviceMonitorParamModel model : models) {

            int pointNum = defaultPointNum;

            try {
                pointNum = Integer.parseInt(model.getNumPoint());
            }
            catch (Exception e) {
            }

            pollNum.put(model.getParamCodeBus(), pointNum);
            polls.add(model.getParamCodeBus());
        }
    }

    /**
     * 数据小数点格式化 gislin 2016年5月26日
     * 
     * @param list
     * @return
     */
    public MonitorDataModel numPointFormat(MonitorDataModel dataModel) {

        String[] fieldPrixs = new String[] { "MEAS_RE", "MEAS_ZS" };

        List<Row> list = dataModel.getData();

        for (String poll : polls) {
            for (String fieldPrix : fieldPrixs) {
                String fieldName = fieldPrix + "_" + poll;
                for (Row row : list) {
                    if (row == null) {
                        continue;
                    }
                    Object value = row.get(fieldName);
                    String finalValue = numPointFormat(poll, value);
                    row.put(fieldName, finalValue);
                }
            }
        }

        return dataModel;
    }

    /**
     * 测量数值格式化，单个参数 gislin 2016年5月26日
     * 
     * @param poll
     * @param value
     * @param waterType
     *            系统类型
     * @return
     */
    public String numPointFormat(String poll, Object value) {

        if (value == null) {
            return null;
        }

        BigDecimal doubleValue = null;

        try {
            doubleValue = new BigDecimal(value.toString());
        } 
        catch (Exception e) {
            return value.toString();
        }

        DecimalFormat df0 = new DecimalFormat("#0");
        DecimalFormat df1 = new DecimalFormat("#0.0");
        DecimalFormat df2 = new DecimalFormat("#0.00");
        DecimalFormat df3 = new DecimalFormat("#0.000");

        Integer pointNum = pollNum.get(poll.toUpperCase());
        String finalValue = null;

        if (pointNum == null) {
            finalValue = df2.format(doubleValue.doubleValue());
            return finalValue;
        }
        if (pointNum == 0) {
            finalValue = df0.format(doubleValue.doubleValue());
        } 
        else if (pointNum == 1) {
            finalValue = df1.format(doubleValue.doubleValue());
        } 
        else if (pointNum == 2) {
            finalValue = df2.format(doubleValue.doubleValue());
        } 
        else if (pointNum >= defaultPointNum) {
            finalValue = df3.format(doubleValue.doubleValue());
        }
        return finalValue;
    }

    /**
     * 给报表添加报警信息 gislin 2016年8月5日
     * 
     * @param list
     *            列表数据
     * @param parms
     *            要分析的污染物集合
     * @param mn
     *            设备编号
     * @return
     */
    public List<Row> generateAlarmInfo(List<Row> list, String[] parms, String mn) {

        return list;
    }
}
